<script setup>
import { ref } from 'vue';
import {ElForm, ElInput, ElButton, ElMessage} from 'element-plus';
import login_png from '@/assets/logo.png'; // 复用登录页的图片
import { useRouter } from 'vue-router';
import login from '../../api/login.js'
import {useLoginStore} from "@/stores/login.js";
const loginStore = useLoginStore();

// 3. 注册表单实例（传递给 Pinia 的校验方法）
const registerFormRef = ref();

// 注册逻辑
const handleRegister = async () => {
  await loginStore.userRegister(registerFormRef.value);
};

</script>


<template>
  <div class="login-page">
    <!-- 外层容器 -->
    <el-container class="login-container">
      <!-- 左侧图片区域（复用登录页的图片） -->
      <el-aside width="50%" class="hidden md:flex login-aside">
        <img
            :src="login_png"
            alt="注册背景图"
            class="login-aside__img"
        >
      </el-aside>

      <!-- 右侧注册表单区域 -->
      <el-main class="login-main">
        <div class="login-main__inner">
          <div class="title-container">
            <h1>易购管理系统</h1>
            <p class="subtitle">创建新账号</p>
          </div>

          <!-- 注册表单 -->
          <el-form
              ref="registerFormRef"
              :model="loginStore.registerForm"
              :rules="loginStore.rules"
              label-width="0"
              class="login-form"
          >
            <el-form-item prop="username">
              <el-input
                  v-model="loginStore.registerForm.username"
                  placeholder="用户名"
                  prefix-icon="User"
                  class="login-form__input"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                  v-model="loginStore.registerForm.password"
                  type="password"
                  placeholder="密码"
                  prefix-icon="Lock"
                  class="login-form__input"
              ></el-input>
            </el-form-item>

            <el-form-item prop="confirmPassword">
              <el-input
                  v-model="loginStore.registerForm.confirmPassword"
                  type="password"
                  placeholder="确认密码"
                  prefix-icon="Lock"
                  class="login-form__input"
              ></el-input>
            </el-form-item>

            <el-form-item class="button-container">
              <el-button
                  type="primary"
                  @click="handleRegister"
                  :loading="loading"
                  class="el-button"
              >
                注册
              </el-button>
              <el-button
                  type="primary"
                  @click="loginStore.goToLogin"
                  :loading="loading"
              >
                返回登录
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-main>
    </el-container>

    <!-- 页脚 -->
    <el-footer class="login-footer">
      <p>© 2025 易购管理系统 版权所有</p>
    </el-footer>
  </div>
</template>



<style scoped>
/* 复用登录页的基础样式，可按需调整 */
.login-page {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background-color: #f5f7fa;
}

.login-container {
  width: 100%;
  height: calc(100% - 60px);
  max-width: 1200px;
  margin: 0 auto;
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-aside {
  overflow: hidden;
}
.login-aside::-webkit-scrollbar {
  display: none;
}
.login-aside__img {
  width: 100%;
  height: 100%;
}

.title-container {
  text-align: center;
  margin-bottom: 24px;
}
.subtitle {
  color: #999;
  font-size: 14px;
  margin-top: 8px;
}

.login-main {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-main__inner {
  width: 100%;
  max-width: 400px;
}

.login-form {
  width: 100%;
}
.login-form__input {
  height: 48px;
  border-radius: 8px;
  margin-bottom: 20px;
}

.button-container {
  text-align: center;
}
.el-button {
  margin: 0 8px;
}

.login-footer {
  height: 60px;
  line-height: 60px;
  text-align: center;
  color: #666;
  font-size: 14px;
  background-color: #fff;
}
</style>